<?php
namespace app\admin\controller;
use think\Controller;
class Users extends Common
{
	//注册用户信息显示
	public function index(){
		$all = input('get.all');
		$vip = input('post.vip');
		$data = model('Users')->index();
		$this->assign('all',$all);
		$this->assign('vip',$vip);
		$this->assign('data',$data);
		return $this->fetch();
	}

	//总代理显示
	public function indexusers(){
		$data = db('users')->where('pid',0)->order('uid desc')->select();
		$this->assign('data',$data);
		return $this->fetch();
	}

	public function see(){
       $uid = input('param.uid');
       $data = db('users')->where('uid',$uid)->find();
       $msg = db('users')->field('username')->where('pid',$uid)->select();
       $users = array();
       foreach ($msg as $key => $value) {
       	     $users[] = $value['username'];
       };
       $users = join(' , ',$users);
       $this->assign('data',$data);
       $this->assign('users',$users);
       return $this->fetch();
    }

	//添加总代理压面
	public function addsole(){
		return $this->fetch();
	}

	//添加总代理
	public function addsoles(){
		if(request() -> isPost()){
			$data = model('Users')->addsole();
			if($data == 1){
				$this->success('添加成功','admin/Users/indexusers','',1);
			}elseif($data == 4){
				$this->error('添加失败,你还未同意协议','admin/users/addsole','',1);
				exit;
			}else{
				$this->error('已经存在总代理,不能进行添加','admin/users/indexusers','',1);
				exit;
			}
		}else{
			$this->error('请求失败');
				exit;
		}
	}

	//添加注册用户页面
	public function addusers(){
        return $this->fetch();
	}

	//添加注册用户
	public function adduserss(){
		if(request() -> isPost()){
			$data = model('Users')->addusers();
			if($data == 1){
				$this->success("添加成功",'admin/Users/index','',1);
			}elseif($data == 602){
				$this->error('用户名存在,请重新输入','admin/users/addusers','',1);
				exit;
			}elseif($data == 4){
				$this->error('添加失败,你还未同意协议','admin/users/addusers','',1);
				exit;
			}elseif($data == 604){
	            $this->error('添加失败,邀请码不存在','admin/users/addusers','',1);
				exit;
			}else{
				$this->error('addusers','admin/users/addusers','',1);
				exit;
			}	
		}else{
			$this->error('请求失败');
				exit;
		}
		
	}
	//用户信息修改页面 bf7ace1d0309e525681a49703fbc0cc0
	public function edit(){
		$uid = input('param.uid');
		$data = db('users')->where('uid',$uid)->find();
		$dailidata = db('users')->where('vip',2)->select();
		
		$this->assign('data',$data);
		$this->assign('dailidata',$dailidata);
		return $this->fetch();
	}

	//用户信息修改
	public function edits(){
		if(request()->isPost()){
			$edit = model('Users')->edit();
			if($edit['valid']){
                   $this->success($edit['msg'],'admin/Users/index','',1);
                    exit;
                }else{
                    $this->error($edit['msg'],'','',1);
                    exit;
            }

		}else{
			$this->error('请求失败');
		}
	}

	//用户金额充值
	public function topup(){
		$uid = input('param.uid');
		$data = db('users')->field('username')->where('uid',$uid)->find();
		$this->assign('data',$data);
		return $this->fetch();
	}

	//用户金额充值
	public function topups(){
		if(request() -> isPost()){
            $uid = input('param.uid');
			$money = input('post.money');
			$user_name = session('user_name');
			$username = input('post.username');
			$createtime = date('Y-m-d H:i:s');
			$usermoney = db('users')->where('uid',$uid)->find();
			if(($usermoney['money']+$money)<0){
				$this->error("金额不足,充值失败");
			}
	        $moneys = db('users')->where('uid',$uid)->setInc('money', $money);
		    if($moneys){
		    	$content = '金额充值';
        	    admin_log($content); 
		    	$data = array(
		    		'user_name' => $user_name,
		    		'username' => $username,
		    		'money' => $money,
		    		'createtime' => $createtime,
		    	);
		    	db('pay')->insert($data);
	            $this->success('充值成功','admin/users/index','',1);
		    }else{
		    	$this->error('充值失败','','',1);
		    }
		}else{
           $this->error('请求失败','','',1);
		}
	}

	//随机生成邀请码
	public function code(){
		$uid = input('param.uid');
		$data['icode1'] = getRandomString();
        $data['icode2'] = getRandomString();
        $codes = db('users')->field('icode1,icode2')->where('icode1',$data['icode1'])->whereOr('icode1',$data['icode2'])->whereOr('icode2',$data['icode1'])->whereOr('icode2',$data['icode2'])->find();
        if(($data['icode1'] != $data['icode2']) && !isset($codes)){
        	 $data = db('users')->where('uid',$uid)->update($data);
        	 if($data){
                $this->redirect('admin/users/index');
        	 }else{
        	 	$this->error('邀请码生成失败,请重新生成');
        	 }
		
        }else{
        	$this->error('邀请码生成失败');
        }	
	}

	//返水率
	public function returns(){
		$uid = input('param.uid');
		$data = db('users')->where('uid',$uid)->find();
		$this->assign('data',$data);
		return $this->fetch();
	}

	//返水率设置
	public function returnss(){
		if(request() -> isPost()){	
			$uid = input('param.uid');
	        $pid = input('post.pid');
		    $pid = db('users')->field('return')->where('uid',$pid)->find();
			$msg['return'] = input('post.return');
			if($pid == 0){
                $data = db('users')->where('uid',$uid)->update($msg);
				if($data){
					$this->success('设置成功','admin/users/index','',1);
				}else{
		            $this->error('设置失败');
				}   		
            }elseif($msg['return'] >= $pid['return']){   
                   $this->error('返水率不能大于或等于推荐人的返水');
			}else{
				$data = db('users')->where('uid',$uid)->update($msg);
				if($data){
					$this->success('设置成功','admin/users/index','',1);
				}else{
		            $this->error('设置失败');
				}
			}
		}else{
			$this->error('请求失败');
		}
	}

	//删除会员
	public function del(){
		$uid = input('param.uid');
		$del = db('users')->where('uid',$uid)->delete();
		if($del > 0){
				$content = '删除会员列表';
	        	admin_log($content); 
				$this->success('删除成功','index','',1);
		}else{
				$this->error('删除失败','','',1);
	    }
	}
}